//
//  RCBalanceVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-7-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCBalanceVC.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "GloableEngineService.h"
#import "IManager+Balance.h"
#import "GloableEngineService.h"

@interface RCBalanceVC ()<IManagerDelegate>

@property(nonatomic,assign) int64_t requestID;
@property(nonatomic,retain) MBProgressHUDEx *progressHub;

@end

@implementation RCBalanceVC

@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc{
    
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"我的积分";
    self.navigationItem.titleView = [self createTitleView];
    
    UILabel *nickName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    nickName.text = [[GloableEngineService sharedInstance] nickName];
    [nickName setFont:[UIFont fontWithName:kFontNameLightHeiti size:15]];
    [nickName setTextColor:[UIColor colorWithHex:0xff505050]];
    [nickName setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    [nickName setTextAlignment:NSTextAlignmentCenter];
    [self.viewContainer addSubview:nickName];
    [nickName release];
    
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    NSString *bduss = [[GloableEngineService sharedInstance] bduss];
    _requestID = [[IManager sharedInstance] requestBlance:bduss withDelegate:self];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark Imanager Delegate

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        RCBalanceDataModel *balance = (RCBalanceDataModel *)dataModel;
        
        UIImageView *bg1 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 50+12, 320-14*2, 47)] autorelease];
        UIImage *img1 = [UIImage imageNamed:@"form_bg3"];
        bg1.image = img1;
        [self.viewContainer addSubview:bg1];
        
        UIImage *img = [UIImage imageNamed:@"icon_balance_indicator"];
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(30, 80, img.size.width, img.size.height)];
        icon.image = img;
        [self.viewContainer addSubview:icon];
        [icon release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 80, 200, 14)];
        [label setBackgroundColor:[UIColor clearColor]];
        label.font = [UIFont fontWithName:kFontNameHeiti size:13];
        [self.viewContainer addSubview:label];
        label.textColor = [UIColor colorWithHex:0xff505050];
        label.text = [NSString stringWithFormat:@"我的积分：%@",balance.balance];
        [label release];
    }
    
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
}
@end
